import requests
import re
import MySQLdb

def login(session, login_url, username, password, success=1):
    """
    登陆功能
    :param session:保持会话参数
    :param login_url:登陆网址url
    :param username:登陆用户名
    :param password:登陆密码
    :param success:登陆状态
    :return:返回登陆状态，True表示登陆成功，False表示登陆失败
    """
    ret = False
    form_data = {"username": username, "password": password}
    resp = session.post(login_url, data=form_data)
    if resp.json()["code"] == success:
        ret = True
    return ret


def sign_in(session, sign_url, success = 1):
    """
    签到功能
    :param session: 保持会话参数
    :param sign_url: 签到网址url
    :param success:签到状态
    :return:返回签到状态，True表示签到成功，False表示签到失败
    """
    ret = False
    resp = session.post(sign_url)
    if resp.json()["code"] == success:
        ret = True
    return ret


def get_pay_uid(session, pay_url, pay_type=0, pay_date=0, success = 1):
    """
    支付功能
    :param session: 保持会话参数
    :param pay_url: 支付页面url
    :param pay_type: 支付订单类型
    :param pay_date: 支付订单时长
    :param success:支付状态
    :return:返回用户uid
    """
    pay_data = {"tag": pay_type, "date": pay_date}
    resp = session.post(pay_url, data=pay_data).json()
    uid = ""
    if resp["code"] == success:
        uid = resp["msg"]
    return uid


def hb_pay(session, hb_py_url, uid):
    """
    :param session: 保持会话参数
    :param hb_py_url: 红包支付url
    :param uid: 用户uid
    :return: 返回红包支付结果
    """
    hb_data = {"uid": uid}
    resp = session.post(hb_py_url, data=hb_data)
    return resp


def get_order(session, order_url):
    """
    :param session: 保持会话参数
    :param order_url: 订单套餐网址
    :return: 返回订单id
    """
    resp = session.get(order_url).text
    order_id = re.search('->  (\d+)</option>', resp).group(1)
    return order_id


def get_ips(session, order_id):
    """
    获取api返回ip数据
    :param session: 保持会话参数
    :param order_id: 订单id
    :return:返回IP
    """
    api = f"http://ip.mengdaili.com/get?orderid={order_id}&count=50&datatype=json"
    ip = session.get(api).text
    return ip



